//knockout视图模型初始化
function viewModel(configip, configport) {
	var self = this;
	//ip && port
	self.serverip = ko.observable(configip);
	self.serverport = ko.observable(configport);
	self.isBtnEnable = ko.observable();
	self.serverTest = function() {
		maTestAction();

	}
}

//按钮“MA测试连接”click事件处理
function maTestAction() {
	//保存用户输入的ip/port
	ipset();

	var username = "maclient";
	var password = "123456";

	var jsonobj = {
		username: username,
		password: password
	};

	summer.callAction({
		"appid": "PlaceOrder", //暂时只能用这个appid
		"viewid": "com.yonyou.placeorder.MAConnect",
		"action": "checkLogin",
		"params": jsonobj,
		"callback": "testSuccess()",
		"error": "testError()"
	});
	UM.showLoadingBar({
		text: "正在连接..."
	})
}

function ipset() {
	var currentip = $("#server").val();
	var currentport = $("#port").val();
	summer.writeConfig({
		"host": currentip,
		"port": currentport
	});
}

function ipStorage(){
	var currentip = $("#server").val();
	var currentport = $("#port").val();
	
	summer.setStorage("host", currentip);
	summer.setStorage("port", currentport);
}

function testSuccess(args) {
	UM.hideLoadingBar();
	ipStorage();
	summer.toast({
		msg: "MA连接成功！"
	});
}

function testError(args) {
	UM.hideLoadingBar();
	UM.alert({
		title:"连接错误",
		text: args.err_msg});
}

$(function() {
	//getStorage,读取用户输入过的ip:port
	var configip = summer.getStorage("host") ? summer.getStorage("host") :"";
	var configport = summer.getStorage("port") ? summer.getStorage("port") :"";;
	var vm = new viewModel(configip, configport);
	ko.applyBindings(vm);
});